package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import model.Meal;
import java.util.ArrayList;

import java.util.List;

public class MealDAO extends ModelDAO {

	private Connection connection;
	
	public MealDAO (){
		super();
		connection = getConnection();
	}
	
	public void closeConnection(){
		super.closeConnection();
	}
	
	public Meal select(String name) {
		Meal m = null;
		try{
			String consulta = "SELECT * FROM MEALS WHERE NAME=?";
			PreparedStatement statement = this.connection.prepareStatement(consulta);
			
			try{statement.setString(1, name);}
			catch(Exception e){e.printStackTrace();}
			
			ResultSet result = statement.executeQuery();
			m = new Meal();
			while(result.next()){
				m.setName(result.getString(1));
				m.setTitle(result.getString(2));
				m.setDescription(result.getString(3));
				m.setPrice(result.getInt(4));
				m.setImg_url(result.getString(5));
				m.setType(result.getString(6));
				m.setActive(result.getInt(7));
			}
		    result.close();
		    statement.close();
		}
		catch(SQLException e){e.printStackTrace();}
		return m;
	}
	
	public void create(Meal m){
		if(select(m.getName())!=null)return;
		
		try{
			String consulta = "INSERT INTO MEALS "
				+"(NAME, DESCRIPTION, IMG_URL, TITLE, TYPE, PRICE)"
				+ "VALUES " + "(?,?,?,?,?,?)";
			PreparedStatement statement = this.connection.prepareStatement(consulta);
			
			try{statement.setString(1, m.getName());}
			catch(Exception e){e.printStackTrace();}
			
			if(m.getDescription()!=null)statement.setString(2,m.getDescription());
			else {statement.setNull(2, java.sql.Types.VARCHAR);}
			
			if(m.getImg_url()!=null)statement.setString(3,m.getImg_url());
			else {statement.setNull(3, java.sql.Types.VARCHAR);}
			
			if(m.getTitle()!=null)statement.setString(4,m.getTitle());
			else {statement.setNull(4, java.sql.Types.VARCHAR);}
			
			if(m.getType()!=null)statement.setString(5,m.getType());
			else {statement.setNull(5, java.sql.Types.VARCHAR);}
			
			try{statement.setInt(6, m.getPrice());}
			catch(Exception e){e.printStackTrace();}
			
		    statement.executeUpdate();
		    statement.close();
		}
		catch(SQLException e){e.printStackTrace();}	
	}

	public void update(Meal m){
		if(select(m.getName())==null)create(m);
		
		try{
			String consulta = "UPDATE MEALS "+
				"SET DESCRIPTION = ?, IMG_URL = ?, TITLE = ?, TYPE = ?, PRICE = ?"+
				" WHERE NAME = ?";
			PreparedStatement statement = this.connection.prepareStatement(consulta);

			if(m.getDescription()!=null)statement.setString(1,m.getDescription());
			else {statement.setNull(1, java.sql.Types.VARCHAR);}
			
			if(m.getImg_url()!=null)statement.setString(2,m.getImg_url());
			else {statement.setNull(2, java.sql.Types.VARCHAR);}
			
			if(m.getTitle()!=null)statement.setString(3,m.getTitle());
			else {statement.setNull(3, java.sql.Types.VARCHAR);}
			
			if(m.getType()!=null)statement.setString(4,m.getType());
			else {statement.setNull(4, java.sql.Types.VARCHAR);}
			
			try{statement.setInt(5, m.getPrice());}
			catch(Exception e){e.printStackTrace();}
			
			try{statement.setString(6, m.getName());}
			catch(Exception e){e.printStackTrace();}
			
			statement.executeUpdate();
		    statement.close();
		}
		catch(SQLException e){e.printStackTrace();}
	}
	
	public List<Meal> getAll(){
		List<Meal> meals = new ArrayList<Meal>();
		try{
			Statement statement = this.connection.createStatement();
			String consulta = "SELECT * FROM MEALS";
			ResultSet results = statement.executeQuery(consulta);
			while(results.next()){
				Meal m = new Meal();
				m.setName(results.getString(1));
				m.setTitle(results.getString(2));
				m.setDescription(results.getString(3));
				m.setPrice(results.getInt(4));
				m.setImg_url(results.getString(5));
				m.setType(results.getString(6));
				if(m.getActive()==1)
					meals.add(m);
			}
			results.close();
			statement.close();
		}
		catch(SQLException e){e.printStackTrace();}
		return meals;
	}
	
	public void delete(String name){
		Meal m = select(name);
		m.setActive(0);
		update(m);
	}
}
